// mescroll-body 和 mescroll-uni 通用

// import MescrollUni from "./mescroll-uni.vue";
// import MescrollBody from "./mescroll-body.vue";

const MescrollMixin = {
	// components: { // 非H5端无法通过mixin注册组件, 只能在main.js中注册全局组件或具体界面中注册
	// 	MescrollUni,
	// 	MescrollBody
	// },
	data() {
		return {
			downOption: {
				
				auto: false //是否在初始化后,自动执行downCallback; 默认true
			},
			mescroll: null ,//mescroll实例对象，
			getConfig:null,
			userInfo: uni.getStorageSync("userInfo"),
			
		}
	},
	// 加这里
	onShow (){
		
	},
	onLoad(options) {
		
		this.configFn() 
		
	},
	// 注册系统自带的下拉刷新 (配置down.native为true时生效, 还需在pages配置enablePullDownRefresh:true;详请参考mescroll-native的案例)
	onPullDownRefresh(){
		this.mescroll && this.mescroll.onPullDownRefresh();
	},
	// 注册列表滚动事件,用于判定在顶部可下拉刷新,在指定位置可显示隐藏回到顶部按钮 (此方法为页面生命周期,无法在子组件中触发, 仅在mescroll-body生效)
	onPageScroll(e) {
		this.mescroll && this.mescroll.onPageScroll(e);
	},
	// 注册滚动到底部的事件,用于上拉加载 (此方法为页面生命周期,无法在子组件中触发, 仅在mescroll-body生效)
	onReachBottom() {
		this.mescroll && this.mescroll.onReachBottom();
	},
	methods: {
		
		// mescroll组件初始化的回调,可获取到mescroll对象
		mescrollInit(mescroll) {
			this.mescroll = mescroll;
			this.mescrollInitByRef(); // 兼容字节跳动小程序
		},
		// 以ref的方式初始化mescroll对象 (兼容字节跳动小程序: http://www.mescroll.com/qa.html?v=20200107#q26)
		mescrollInitByRef() {
			if(!this.mescroll || !this.mescroll.resetUpScroll){
				let mescrollRef = this.$refs.mescrollRef;
				if(mescrollRef) this.mescroll = mescrollRef.mescroll
			}
		},
		// 下拉刷新的回调
		downCallback() {
			// mixin默认resetUpScroll
			setTimeout(() => {
				this.mescroll.resetUpScroll()
			}, 1e3);
		},
		// 上拉加载的回调
		upCallback() {
			// mixin默认延时500自动结束加载
			setTimeout(()=>{
				this.mescroll.endErr();
			}, 500)
		},
		async configFn(){
			const result = await this.$http.post({
				url: this.$api.config,
				data: {
			
			
				},
				headers: {
					'Authorization': this.userInfo.token
				}
			
			})
			if (result.code == 0) {
				console.log(result)
				this.getConfig = result.data
				this.signFn(); //查询分享参数
			}

		},
		async signFn() {
			// console.log('https://ship.admin168.net/H5/index.html?id=4#/'.split('#/')[0].split("?")[0])
			// window.location.href.split('#/')[0].split("?")[0]
			const result = await this.$http.post({
				url: this.$api.gsign,
				data: {
					url:window.location.href.split('#/')[0]
		
		
				},
				headers: {
					'Authorization': this.userInfo.token
				}
		
			})
			if (result.code == 0) {
				this.signParams = result.data
				this.$wx.config({
					debug: false,
					appId: this.signParams.appId,
					timestamp: this.signParams.timestamp,
					nonceStr: this.signParams.noncestr,
					signature: this.signParams.sign,
					jsApiList: [
						'updateAppMessageShareData',
						'updateTimelineShareData',
						'onMenuShareTimeline',
						'onMenuShareAppMessage',
						'onMenuShareQQ',
						'onMenuShareWeibo',
						'wx-open-launch-weapp',
						'getLocation',
						'checkJsApi',
						'chooseContact',
						'scanQRCode'
					],
					openTagList: ['wx-open-launch-weapp']
				});
		
				// 获取手机是否开启定位
				this.$wx.ready(() => {
					this.$wx.checkJsApi({
						jsApiList: ['onMenuShareAppMessage', 'onMenuShareTimeline',
							'onMenuShareQQ', 'onMenuShareWeibo'
						],
						success: res => {
							if (res.checkResult.getLocation == false) {
								alert('你的微信版本太低，不支持微信JS接口，请升级到最新的微信版本！' +
									JSON.stringify(res));
								return;
							}
							// alert('成功！' + JSON.stringify(res));
						},
						fail: res => {
							// alert('失败！' + JSON.stringify(res));
						},
						complete: res => {	
		
							// alert('所有' + JSON.stringify(res));
						}
					});
					// 设置分享朋友圈/好友配置——需展示
					this.sharingSettings()
				});
		
			} else {
				this.$sun.toast(result.msg, 'none')
			}
		},
		sharingSettings() {
			// https://dazi.weijuyunke.cn/H5/index.html?invite=wpj98i#/
			// https://ship.admin168.net/home.php/wechatLogin?url=H5/home.html&invite=qzeqev
			// https://ship.admin168.net/pages/shop/shoplDetailsPage/shoplDetailsPage?orderId=1
			
			let linkHref = window.location?.origin +'/H5/index.html?invite=' +  uni.getStorageSync('userInfo').invite_code
			
		
			let params = {
				title: this.getConfig.distribution_title, // 分享标A题
				link: linkHref,
		
				desc: this.getConfig.distribution_instructions, //详情
				imgUrl: this.getConfig.distribution_poster // 分享图标
			};
			this.$wx.onMenuShareAppMessage(params); //分享给好友
			this.$wx.onMenuShareTimeline(params); //分享到朋友圈
			this.$wx.onMenuShareQQ(params); //分享给手机QQ
			this.$wx.onMenuShareQZone(params); //分享给手机QQ空间
			this.$wx.onMenuShareWeibo(params); //分享给微博
		},
	},
	mounted() {
		this.mescrollInitByRef(); // 兼容字节跳动小程序, 避免未设置@init或@init此时未能取到ref的情况
	}
	
}

export default MescrollMixin;
